package com.liam.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.Properties;

/**
 * Created by Administrator on 2016/9/14.
 */
public class PropsUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(PropsUtil.class);

    /**
     * 加载配置文件
     * @param fileName
     * @return
     */
    public static Properties loadProps(String fileName){
        Properties props = null;
        InputStream is = null;
        try {
            is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
            props = new Properties();
            props.load(is);
        }catch (Exception e){
            LOGGER.error(e.getMessage());
        }finally {
            if(is != null){
                try {
                    is.close();
                }catch (Exception e){
                    LOGGER.error(e.getMessage());
                }
            }
        }
        return props;
    }

    public static String getString(Properties props ,String key){
        return getString(props,key,"");
    }

    public static String getString(Properties props, String key, String defaultValue) {
        String value = defaultValue;
        if(props.contains(key)){
            value = props.getProperty(key);
        }
        return value;
    }


}
